package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐信息
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
   @Autowired
   private SetmealMapper setmealMapper;
   @Autowired
   private SetmealDishService setmealDishService;
    /**
     * 分页查询套餐信息(包括分类信息)
     * @param page,pageSize 分页条件
     * @param name 查询条件
     * @return 查询结果
     */
   @Override
    public Page<SetmealDto> pageSetmealDto(int page, int pageSize, String name) {
        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
        return setmealMapper.pageSetmealDto(setmealDtoPage,name);
    }

    /**
     * 添加套餐信息 ( 套餐关联的菜品集合)
     * @param setmealDto 套餐信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSetmealDto(SetmealDto setmealDto) {
        //添加套餐表信息
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //添加 套餐id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        //添加关联菜品表信息
        setmealDishService.saveBatch(setmealDishes );
    }

    /**
     * 根据id删除套餐 (包括套餐关联的菜品集合)
     * @param ids 套餐id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIdWithDish(Long[] ids) {
        //查询要删除的套餐 状态 禁售状态无法删除
        Integer count = this.lambdaQuery()
                .in(Setmeal::getId, Arrays.asList(ids))
                .eq(Setmeal::getStatus, 1) // 启售状态
                .count();
        if (count > 0){
            throw new CustomException("套餐正在启售,无法删除");
        }
        //删除套餐信息
        this.removeByIds(Arrays.asList(ids));

        //删除套餐关联的菜品集合
        setmealDishService.lambdaUpdate()
                .in(SetmealDish::getSetmealId, Arrays.asList(ids))
                .remove();
    }

    /**
     * 根据id查询套餐信息 ( 包括套餐关联的菜品集合)
     * @param id 套餐id
     * @return 查询结果
     */
    @Override
    public SetmealDto findByIdWithDish(Long id) {
        return setmealMapper.findByIdWithDish(id);
    }

    /**
     * 修改套餐信息 ( 关联菜品集合)
     * @param setmealDto 套餐信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmealDto(SetmealDto setmealDto) {
        //修改套餐信息表
        this.updateById(setmealDto);
        //删除关联菜品信息表

        setmealDishService.lambdaUpdate()
                .eq(SetmealDish::getSetmealId, setmealDto.getId())
                .remove();
        //修改关联菜品表信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //添加 套餐id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据分类 查询套餐信息 ( 套餐关联的菜品)
     * @param setmeal 分类信息
     * @return 查询结果
     */
    @Override
    public List<SetmealDto> listSetmealDto(Setmeal setmeal) {
        //根据 分类id 查询套餐信息
        List<Setmeal> setmealList = this.lambdaQuery()
                .eq(Setmeal::getStatus, setmeal.getStatus()) //查询 启售的套餐
                .eq(Setmeal::getCategoryId, setmeal.getCategoryId()) //查询套餐信息
                .orderByDesc(Setmeal::getUpdateTime)
                .list();

        List<SetmealDto> setmealDtos = setmealList.stream().map(setmeal1 -> {
            SetmealDto setmealDto = new SetmealDto();
            //复制对象
            BeanUtils.copyProperties(setmeal1, setmealDto);
            //查询 套餐关联的菜品
            List<SetmealDish> setmealDishList = setmealDishService.lambdaQuery()
                    .eq(SetmealDish::getSetmealId, setmeal1.getId())
                    .orderByDesc(SetmealDish::getUpdateTime)
                    .list();
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());

        return setmealDtos;
    }

    /**
     * 根据id查询套餐关联的菜品
     * @param id 套餐id
     * @return 查询结果
     */
    @Override
    public List<Dish> findDishById(Long id) {
        return setmealMapper.findDishById(id);

    }


}
